/**
 * @date 2006-8-28
 */

package org.nanhill.commons.lang;

/**
 * @author alin [xalinx at gmail dot com]
 * @date 2006-8-28
 */
public class FillEmptyStringProcessor implements StringProcessor {
	private int max;

	private static final String defaultStr = "____________________";

	private static final int defaultLen = 20;

	public FillEmptyStringProcessor(int max) {
		this.max = max;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.nanhill.core.StringProcessor#process(java.lang.String)
	 */
	public String process(String in) {
		int len = max - in.length();
		int div = len / defaultLen;
		int mod = len % defaultLen;
		if (div > 0) {
			StringBuilder builder = new StringBuilder(in);
			for (int i = 0; i < div; i++) {
				builder.append(defaultStr);
			}
			builder.append(defaultStr.substring(0, mod));
			return builder.toString();
		} else {
			return in + defaultStr.substring(0, mod);
		}
	}

}
